//获取当前页面实例
export function currentPage() {
    const pages = getCurrentPages()
    const currentPage = pages[pages.length - 1]
    return currentPage || {}
}
//上传图片
export function chooseImage() {
	uni.chooseImage({
		sourceType: ['album'],
		count: 6,
		success: (chooseImageRes) => {
			const tempFilePaths = chooseImageRes.tempFilePaths;
			console.log(tempFilePaths);
			this.tempFilePaths = tempFilePaths;
			for (let i = 0; i < tempFilePaths.length; i++) {
				if (i < tempFilePaths.length) {
					var arr = [];
					var arrs = []
					uni.uploadFile({
						url: 'https://shejiao.yunjingwl.com/index.php/api/file.upload/image',
						filePath: tempFilePaths[i],
						name: 'iFile',
						formData: {
							token: uni.getStorageSync('token'),
							app_id: 10001
						},
						success: (uploadFileRes) => {
							console.log(uploadFileRes, 'upload');
							var data = JSON.parse(uploadFileRes.data);
							console.log(data, 'data')
							arr.push(data.data.file_path);
							arrs.push({
								file_id: data.data.file_id,
								file_path: data.data.file_path
							})
							console.log(arr, 'arr')
							this.imgpath = arr;
							this.image = arrs
							console.log(this.imgpath, 'imgpath');
						}
					});
				}
			}
		}
	});
}
//保存图片
export function downImg(){
	// #ifdef H5
	uni.showToast({
		title:'长按保存图片',
		icon:'none',
		duration:1500
	})
	return
	// #endif
	// #ifdef MP-WEIXIN
	uni.showLoading({
		title: '加载中'
	});
	var that = this
	uni.downloadFile({
		url: that.hbOne,//图片地址
		success: function (res) {
			console.log(res);
			uni.saveImageToPhotosAlbum({
				filePath: res.tempFilePath,
				success: function (data) {
						uni.showToast({
							title:'图片保存到相册',
							icon:'none',
							duration:1000
						})
				},
				fail: function (err) {
					
					if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny" || err.errMsg === "saveImageToPhotosAlbum:fail authorize no response") {
						// 这边微信做过调整，必须要在按钮中触发，因此需要在弹框回调中进行调用
						if(that.fails){
							return
						}
						uni.showModal({
							title: '提示',
							content: '需要您授权保存相册',
							showCancel: false,
							success: modalSuccess => {
								uni.openSetting({
									success(settingdata) {
										console.log("settingdata", settingdata)
										if (settingdata.authSetting['scope.writePhotosAlbum']) {
											that.fails = true
											uni.showModal({
												title: '提示',
												content: '获取权限成功,再次点击即可保存',
												showCancel: false,
											})
										} else {
											uni.showModal({
												title: '提示',
												content: '获取权限失败，将无法保存到相册哦~',
												showCancel: false,
											})
										}
										wx.hideLoading()
									},
									fail(failData) {
										console.log("failData", failData)
										wx.hideLoading()
									},
									complete(finishData) {
										console.log("finishData", finishData)
									}
								})
							}
						})
					}
				},
				complete(res) {
					wx.hideLoading()
				}
			})
		},
		fail:function(err){
			console.log(err);
			wx.hideLoading()
		}
	})
	// #endif
}
export function phonejy(e){
	
	let reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
	if (!reg.test(e) || e == "") {
		
		uni.showToast({
			title: '请输入正确的手机号码！',
			icon: 'none',
			duration: 700
		})
		return
	}
}
//预览头像
export function previewimage(e) {
	var prearr = []
	prearr.push(e)
	uni.previewImage({
		urls: prearr,
		current: 0,
	});
}
//多张预览
export function previewimages(e, index2) {
	// var index = index;
	var prearr = []
	for (var i = 0; i < e.length; i++) {
		prearr.push(e[i].file_path)
	}
	uni.previewImage({
		urls: prearr,
		current: index2,
	});
}
//app分享微信朋友圈
export const sharepeople = function(titles='我是标题',des='描述',img=''){
		let that = this
		let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
		let curRoute = routes[routes.length - 1].$page.fullPath // 获取当前页面路由，也就是最后一个打开的页面路由
		var pid = uni.getStorageSync('mid')
		uni.share({
			provider: "weixin", //分享服务提供商（即weixin|qq|sinaweibo）
			scene: 'WXSceneTimeline',//场景，可取值参考下面说明。
			type: 0, //分享形式
			href: `https://shop.miduoyungou.shop/shop?pid=${pid}`, //跳转链接
			title: titles, //分享内容的标题
			summary: des, //分享内容的摘要
			imageUrl: img, 
			success: function(res) {
				 //成功后执行操作
				 uni.showToast({
					title: '分享成功',
					icon: 'none',
					duration: 2000
				})
				
			},
			fail: function(err) {
				 //失败后执行操作
				uni.showToast({
					title: '分享失败',
					icon: 'none',
					duration: 2000
				})
			}
		});
}
//app分享微信好友
export const sharepyq = function(title='我是标题',des='描述',img=''){
	let that = this
		let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
		let curRoute = routes[routes.length - 1].$page.fullPath // 获取当前页面路由，也就是最后一个打开的页面路由
		var pid = uni.getStorageSync('mid')
		uni.share({
			provider: "weixin", //分享服务提供商（即weixin|qq|sinaweibo）
			scene: 'WXSceneSession',//场景，可取值参考下面说明。
			type: 0, //分享形式
			href: `https://shop.miduoyungou.shop/shop?pid=${pid}`, //跳转链接
			title: title, //分享内容的标题
			summary: des, //分享内容的摘要
			imageUrl: img, 
			success: function(res) {
				//成功后执行操作
				 uni.showToast({
					title: '分享成功',
					icon: 'none',
					duration: 2000
				})
			},
			fail: function(err) {
				//失败后执行操作
				uni.showToast({
					title: '分享失败',
					icon: 'none',
					duration: 2000
				})
			}
		});
}

export default {sharepeople,sharepyq,currentPage,previewimage,previewimages,downImg,phonejy,chooseImage}